// HSS.Threading.TimerSchedule.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       TimerSchedule.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
namespace HSS.Threading
{
	#region Using Directives
	using System;
	using System.Collections.ObjectModel;
	using System.Globalization;
	#endregion

	#region TimerSchedule
	/// <summary>
	/// A configuration that can be used to schedule a <see cref="HSS.Threading.Timer"/>
	/// </summary>
	[Serializable]
	public sealed class TimerSchedule : IConfigurationElement
	{
		#region Constants
		/// <summary>
		/// Monday = 2
		/// </summary>
		public const int Monday = 2;
		/// <summary>
		/// Tuesday = 4
		/// </summary>
		public const int Tuesday = 4;
		/// <summary>
		/// Wednesday = 8
		/// </summary>
		public const int Wednesday = 8;
		/// <summary>
		/// Thursday = 16
		/// </summary>
		public const int Thursday = 16;
		/// <summary>
		/// Friday = 32
		/// </summary>
		public const int Friday = 32;
		/// <summary>
		/// Saturday = 64
		/// </summary>
		public const int Saturday = 64;
		/// <summary>
		/// Sunday = 128
		/// </summary>
		public const int Sunday = 128;
		#endregion

		#region Constructor
		/// <summary>
		/// Default Constructor
		/// </summary>
		public TimerSchedule()
		{
			this.Name = "Schedule1";
			this._frequency = ScheduleFrequency.Daily;
			this._occurence = 1;
			this._occurenceFlag = 0;
			this._dailyType = ScheduleDailyType.OnceADay;
			this._onceAt = DateTime.Now;
			this._interval = 30;
			this._intervalFrequency = ScheduleIntervalFrequency.Continuous;
			this._intervalBegins = DateTime.MinValue;
			this._intervalEnds = DateTime.MinValue;
			this._description = "Default timer. Daily, occuring once a day at " + this._onceAt.TimeOfDay.ToString();
		}
		/// <summary>
		/// Creates a new Schedule set to occuring once a day at the given time with a frequency of Daily.
		/// </summary>
		/// <param name="timeOfDay">The time of day in which the timer should execute</param>
		public TimerSchedule(DateTime timeOfDay)
		{
			this.Name = "Schedule1";
			this._frequency = ScheduleFrequency.Daily;
			this._occurence = 1;
			this._occurenceFlag = 0;
			this._dailyType = ScheduleDailyType.OnceADay;
			this._onceAt = timeOfDay;
			this._interval = 30;
			this._intervalFrequency = ScheduleIntervalFrequency.Continuous;
			this._intervalBegins = DateTime.MinValue;
			this._intervalEnds = DateTime.MinValue;
			this._description = "Default timer. Frequency of Daily, and occur once a day at " + this._onceAt.TimeOfDay.ToString();
		}
		/// <summary>
		/// Creates a new Schedule using parameters specified
		/// </summary>
		/// <param name="id">The UniqueID for the Schedule</param>
		/// <param name="name">The friendly name for this Schedule</param>
		/// <param name="frequency">The Frequency of the timer; Daily, Weekly or Monthly</param>
		/// <param name="occurence">The Occurrence of the timer</param>
		/// <param name="occurenceOn">A flag indicating when the timers occurs</param>
		/// <param name="dailyType">Specify if the timer is to run once per day or at specified intervals through out the day</param>
		/// <param name="onceAt">The one point in time when the timer should run when the daily type is equal to OnceADay</param>
		/// <param name="interval">The interval in which the scheule should repeat given the interval frequency</param>
		/// <param name="intervalFrequency">The frequency of the interval in which the scheule should repeat; Continous or Every Second, Minute or Hour</param>
		/// <param name="intervalBegins">After this point in time, the repeating interval can run</param>
		/// <param name="intervalEnds">After this point in time, the repeating interval can no longer run</param>
		/// <param name="exclusions">Collection of times that are considered exclusions to the timer</param>
		/// <param name="description">Description of the timer</param>
		public TimerSchedule(string id, string name, ScheduleFrequency frequency, int occurence, int occurenceOn,
			ScheduleDailyType dailyType, DateTime onceAt, int interval,
			ScheduleIntervalFrequency intervalFrequency, DateTime intervalBegins,
			DateTime intervalEnds, Collection<ScheduleExclusion> exclusions, string description)
		{
			this.Name = name;
			this._frequency = frequency;
			this._occurence = occurence;
			this._occurenceFlag = occurenceOn;
			this._dailyType = dailyType;
			this._onceAt = onceAt;
			this._interval = interval;
			this._intervalFrequency = intervalFrequency;
			this._intervalBegins = intervalBegins;
			this._intervalEnds = intervalEnds;
			this._exclusions = exclusions;
			this._description = description;
		}
		/// <summary>
		/// Creates a new Schedule using ScheduleConfiguration specified
		/// </summary>
		/// <param name="scheduleConfig">An existing schedule to copy from.</param>
		public TimerSchedule(TimerSchedule scheduleConfig)
		{
			this.Name = scheduleConfig.Name;
			this._frequency = scheduleConfig.Frequency;
			this._occurence = scheduleConfig.Occurrence;
			this._occurenceFlag = scheduleConfig.OccurrenceFlag;
			this._dailyType = scheduleConfig.DailyType;
			this._onceAt = scheduleConfig.OnceAt;
			this._interval = scheduleConfig.Interval;
			this._intervalFrequency = scheduleConfig.IntervalFrequency;
			this._intervalBegins = scheduleConfig.IntervalBegins;
			this._intervalEnds = scheduleConfig.IntervalEnds;
			this._exclusions = scheduleConfig.Exclusions;
			this._description = scheduleConfig.Description;
		}
		#endregion

		#region Properties
		/// <summary>
		/// The friendly name for the data stored in this object.
		/// </summary>
		public string Name
		{
			get;
			set;
		}
		/// <summary>
		/// The Frequency of the timer; Daily, Weekly or Monthly
		/// </summary>
		public ScheduleFrequency Frequency
		{
			get { return _frequency; }
			set
			{
				_frequency = value;
			}
		} ScheduleFrequency _frequency = ScheduleFrequency.Daily;

		/// <summary>
		/// The Occurrence of the timer
		/// </summary>
		public int Occurrence
		{
			get { return _occurence; }
			set { _occurence = value; }
		} int _occurence = 1;

		/// <summary>
		/// A flag indicating when the timers occurs
		/// </summary>
		public int OccurrenceFlag
		{
			get { return _occurenceFlag; }
			set { _occurenceFlag = value; }
		} int _occurenceFlag = 0;

		/// <summary>
		/// Specify if the timer is to run once per day or at specified intervals through out the day
		/// </summary>
		public ScheduleDailyType DailyType
		{
			get { return _dailyType; }
			set { _dailyType = value; }
		} ScheduleDailyType _dailyType = ScheduleDailyType.OnceADay;

		/// <summary>
		/// The one point in time when the timer should run when the daily type is equal to OnceADay
		/// </summary>
		public DateTime OnceAt
		{
			get { return _onceAt; }
			set { _onceAt = value; }
		} DateTime _onceAt = DateTime.Parse("8:00:00 AM", CultureInfo.CurrentUICulture);

		/// <summary>
		/// The interval in which the scheule should repeat given the interval frequency
		/// </summary>
		public int Interval
		{
			get { return _interval; }
			set { _interval = value; }
		} int _interval = 30;

		/// <summary>
		/// The frequency of the interval in which the scheule should repeat; Continous or Every Second, Minute or Hour
		/// </summary>
		public ScheduleIntervalFrequency IntervalFrequency
		{
			get { return _intervalFrequency; }
			set { _intervalFrequency = value; }
		} ScheduleIntervalFrequency _intervalFrequency = ScheduleIntervalFrequency.Seconds;

		/// <summary>
		/// After this point in time, the repeating interval can run
		/// </summary>
		public DateTime IntervalBegins
		{
			get { return _intervalBegins; }
			set { _intervalBegins = value; }
		} DateTime _intervalBegins = DateTime.Parse("12:00:00 AM", CultureInfo.CurrentUICulture);

		/// <summary>
		/// After this point in time, the repeating interval can no longer run
		/// </summary>
		public DateTime IntervalEnds
		{
			get { return _intervalEnds; }
			set { _intervalEnds = value; }
		} DateTime _intervalEnds = DateTime.Parse("11:59:59 PM", CultureInfo.CurrentUICulture);

		/// <summary>
		/// Collection of times that are considered exclusions to the timer
		/// </summary>
		public Collection<ScheduleExclusion> Exclusions
		{
			get { return _exclusions; }
			set { _exclusions = value; }
		} Collection<ScheduleExclusion> _exclusions = new Collection<ScheduleExclusion>();

		/// <summary>
		/// A description of the timer.
		/// </summary>
		public string Description
		{
			get { return _description; }
			set { _description = value; }
		} string _description;

		#endregion

		#region Methods
		/// <summary>
		/// Returns the ScheduleID
		/// </summary>
		/// <returns>The ScheduleID</returns>
		public override string ToString()
		{
			return this.Name;
		}
		/// <summary>
		/// Initializes an Array of Schedules
		/// </summary>
		/// <param name="schedule">The first Schedule in the array.</param>
		/// <returns>The Schedule Array</returns>
		public static TimerSchedule[] Create(TimerSchedule schedule)
		{
			TimerSchedule[] sc = new TimerSchedule[1];
			sc[0] = new TimerSchedule(schedule);
			return sc;
		}
		#endregion
	}
	#endregion
}